package com.whf.android.jar.util.bitmap;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import com.blankj.utilcode.util.ScreenUtils;
import com.whf.android.jar.util.log.LatteLogger;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * 图片的处理类
 *
 * @author qf
 * @version 1.0.2
 */
public final class BitmapUtils {

    /**
     * 叠加图片（类似添加水印）
     *
     * @param original：底图
     * @param second：水印图
     */
    public static Bitmap addWatermark(Bitmap original, Bitmap second) {
        if (original == null) {
            LatteLogger.e("底图 bitmap is null");
            return null;
        }
        int w = original.getWidth();
        int h = original.getHeight();
        Bitmap result = Bitmap.createBitmap(w, h, original.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(original, 0, 0, null);
        if (second != null) {
            canvas.drawBitmap(second, 0, 0, null);
        }
        return result;
    }


    /**
     * 图片纵向拼接
     *
     * @param original：底图
     * @param second：
     */
    public static Bitmap addBitmap(Bitmap original, Bitmap second) {
        if (original == null || second == null) {
            return null;
        }
        int w = original.getWidth();
        int h = original.getHeight() + second.getHeight();
        Bitmap result = Bitmap.createBitmap(w, h, original.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(original, 0, 0, null);
        canvas.drawBitmap(second, 0, original.getHeight(), null);
        return result;
    }

    /**
     * @param filePath:BitmapFactory从File中解析图片并防止OOM
     */
    public static Bitmap decodeImage(String filePath) {
        File file = new File(filePath);
        Bitmap bitmap = null;
        try {
            // 打开文件
            InputStream fileInputStream = new FileInputStream(file);
            byte[] data = getBytes(fileInputStream);
            // 读取图像数据并转换为Bitmap
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

            // 关闭文件
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    public static byte[] getBytes(InputStream is) throws IOException {
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024]; // 用数据装
        int len = -1;
        while ((len = is.read(buffer)) != -1) {
            outstream.write(buffer, 0, len);
        }
        outstream.close();   // 关闭流一定要记得。

        return outstream.toByteArray();
    }

    /**
     * @param filePath:BitmapFactory从File中解析图片并防止OOM
     */
    public static Bitmap decodeImageSize(String filePath) {
        return decodeImageSize(filePath, false);
    }

    /**
     * @param filePath:BitmapFactory从File中解析图片并防止OOM。
     * @param sample:是否进行压缩处理。
     */
    public static Bitmap decodeImageSize(String filePath, boolean sample) {
        if (sample) {
            final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            // 第一次警析将inJustDecodeBounds设管为true，来获取图片大小
            bitmapOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, bitmapOptions);
            // 通过获文件获得与需要的比例最接近的比例
            bitmapOptions.inSampleSize = calculateInSampleSize(bitmapOptions);
            // 使用获取到的inSampleSize值再次解析图片
            bitmapOptions.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(filePath, bitmapOptions);
        }
        return BitmapFactory.decodeFile(filePath);
    }

    /**
     * 获得与需要的比例最接近的比例
     */
    public static int calculateInSampleSize(BitmapFactory.Options bitmapOptions) {
        int reqWidth = ScreenUtils.getAppScreenWidth();
        int reqHeight = ScreenUtils.getAppScreenHeight();
        return calculateInSampleSize(bitmapOptions, reqWidth, reqHeight);
    }

    /**
     * 获得与需要的比例最接近的比例
     */
    public static int calculateInSampleSize(BitmapFactory.Options bitmapOptions, int reqWidth, int reqHeight) {
        if (bitmapOptions == null) {
            return 1;
        }
        final int height = bitmapOptions.outHeight;
        final int width = bitmapOptions.outWidth;
        int sampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            sampleSize = Math.min(heightRatio, widthRatio);
        }
        return sampleSize;
    }

}













































